﻿using System;
using System.Collections.Generic;
using FacebookWrapper.ObjectModel;

namespace Desktop_Facebook
{
	internal class MessageFilterer
	{
		public DateTime Begin { get; set; } 

		public DateTime End { get; set; }

		public string Keyword { get; set; }

		private Predicate<Message> m_Predicate = null; 

		public List<Message> FilterByDates()
		{
			m_Predicate = isMessageInDateRange;

			return filterByPredicate();
		}

		public List<Message> FilterByText()
		{
			m_Predicate = doesMessageContainString;

			return filterByPredicate();
		}

		public List<Message> AllMessages()
		{
			m_Predicate = allMessages;

			return filterByPredicate();
		}

		private List<Message> filterByPredicate()
		{
			List<FacebookWrapper.ObjectModel.Message> filtered = new List<FacebookWrapper.ObjectModel.Message>();
			
			if (m_Predicate != null)
			{
				foreach (InboxThread currentThread in Session.Instance.LoggedInUser.InboxThreads)
				{
					foreach (Message currentMessasge in currentThread.Messages)
					{
						if (m_Predicate.Invoke(currentMessasge))
						{
							filtered.Add(currentMessasge);
						}
					}
				}
			}

			return filtered;			
		}

		private bool doesMessageContainString(Message i_Message)
		{
			return i_Message.Text.Contains(Keyword);
		}

		private bool isMessageInDateRange(Message i_Message)
		{ 
			return (i_Message.CreatedTime >= Begin) && (i_Message.CreatedTime <= End);
		}

		private bool allMessages(Message i_Message)
		{
			return true;
		}
	}
}
